package com.hd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hd.dao.BookDao;
import com.hd.pojo.Book;
import com.hd.service.BookService;
import com.hd.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;
    @Override
    public JsonData allBook(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Book> books=bookDao.allBook(page,limit);
        PageInfo<Book> pageInfo=new PageInfo<>(books);
        return JsonData.buildSuccess(pageInfo,"success");
    }

    @Override
    public JsonData delBook(int bookId) {
        int i=bookDao.delBook(bookId);
        if(i>0){
            return JsonData.buildSuccess("成功");
        }
        return JsonData.buildError("失败");
    }

    @Override
    public JsonData saveBook(Book book) {
        int i=0;
        if(book.getBookId()==null){
            i=bookDao.saveBook(book);
        }else {
            i=bookDao.enidBook(book);
        }
        if(i>0){
            return JsonData.buildSuccess("成功");
        }
        return JsonData.buildError("失败");
    }
}
